package com.synjones.handsetS8.ild.pages;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.synjones.handsetS8.R;;
import com.synjones.handsetS8.facesdk.FaceAuthAsync;
import com.synjones.handsetS8.facesdk.FaceHandle;
import com.synjones.handsetS8.CardParse2Activity;
import com.synjones.handsetS8.ild.utils.SPUtils;
import com.synjones.reader.IDCardReaderModule;

/**
 * 首页
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public static final int ALGORITHM_VERSION = 2;
    private ImageView ivWorkerReg, ivRoster, ivSecurityEdu;
    private TextView tvLogout;

    public IDCardReaderModule idCardReaderModule;
    private IdCardReadActivity.ReadCardThread ReadCardThreadhandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        ivWorkerReg = (ImageView) findViewById(R.id.iv_reg);
        ivRoster = (ImageView) findViewById(R.id.iv_roster);
        ivSecurityEdu = (ImageView) findViewById(R.id.iv_security_edu);
        tvLogout = (TextView) findViewById(R.id.tv_logout);

        ivWorkerReg.setOnClickListener(this);
        ivRoster.setOnClickListener(this);
        ivSecurityEdu.setOnClickListener(this);
        tvLogout.setOnClickListener(this);

        idCardReaderModule = IDCardReaderModule.getInstance(this);
        new OpenReaderAsync().execute();
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            //退出登录
            case R.id.tv_logout:
                logout();
                return;
            //用工登记
            case R.id.iv_reg:
                intent = new Intent(this, IdCardReadActivity.class);
                break;
            //花名册
            case R.id.iv_roster:
                intent = new Intent(this, WorkerListActivity.class);
                break;
            //安全教育
            case R.id.iv_security_edu:
                //intent = new Intent(this, EduActivity.class);
                //intent = new Intent(this, DemoActivity.class);
                intent = new Intent(this, CardParse2Activity.class);
                break;
            default:
                break;
        }
        startActivity(intent);
    }

    /**
     * 退出登录
     */
    private void logout() {
        SPUtils.remove("accessToken");
        SPUtils.remove("refreshToken");
        startActivity(new Intent(MainActivity.this, LoginActivity.class));
        finish();
    }


    /**
     * 打开读卡器和人脸识别器
     */
    class OpenReaderAsync extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Void... voids) {
            idCardReaderModule.open();
            return idCardReaderModule.isOpen();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //setupProgressDialog("正在打开读卡器，请稍等", null);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            try {
                //progressDialog.dismiss();
            } catch (Exception e) {
            }
            if (!aBoolean) {
                Toast.makeText(MainActivity.this, "读卡器打开失败", Toast.LENGTH_SHORT).show();
            }

            new FaceAuthAsync(MainActivity.this, FaceHandle.getFaceSDKInstance(ALGORITHM_VERSION)).setOnfinish(new FaceAuthAsync.OnFinish() {
                @Override
                public void onDone(boolean initOk, int errCode) {
                    if (!initOk)
                        Toast.makeText(MainActivity.this, "人脸初始化错误，errcode=" + errCode, Toast.LENGTH_SHORT).show();
                }
            }).execute();

        }
    }
}
